package weaver.interfaces.zlldjcws.kjkm.service.impl;


import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.interfaces.zlldjcws.kjkm.bean.KjkmBean;
import weaver.interfaces.zlldjcws.kjkm.service.LdKjkmService;
import weaver.interfaces.zlldjcws.util.Message;

import javax.ejb.Stateless;
import javax.jws.WebService;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ZhangLei
 * @date 2021/8/3
 * @tableName uf_KJKM
 */

@Stateless(mappedName = "LdKjkmServiceImpl")
@WebService(serviceName = "LdKjkmServiceImpl")
public class LdKjkmServiceImpl extends BaseBean implements LdKjkmService {
    @Override
    public Message kjkmData(KjkmBean kjkmBean) {
        Message message = new Message();
        RecordSet rs = new RecordSet();
        writeLog("表字段对象是：");
        writeLog(kjkmBean);
//        判断：1=新增、2=修改、3=删除
        String zflag = kjkmBean.getZflag();
        writeLog("接收到的zflag是：" + zflag);
//        存储即将操作数据库的值
        Map<String, Object> datas = new HashMap<>();
        datas.put("saknr", kjkmBean.getSaknr());
        datas.put("glaccount_type", kjkmBean.getGlaccount_type());
        datas.put("ktoks", kjkmBean.getKtoks());
        datas.put("txt30", kjkmBean.getTxt30());
        datas.put("spras", kjkmBean.getSpras());
        datas.put("txt20", kjkmBean.getTxt20());
        datas.put("txt50", kjkmBean.getTxt50());
        datas.put("sprasen", kjkmBean.getSprasen());
        datas.put("txt20en", kjkmBean.getTxt20en());
        datas.put("txt50en", kjkmBean.getTxt50en());
        datas.put("zflag", kjkmBean.getZflag());
        datas.put("xloev", kjkmBean.getXloev());
        datas.put("bukrs", kjkmBean.getBukrs());
        datas.put("waers", kjkmBean.getWaers());
        datas.put("xmwno", kjkmBean.getXmwno());
        datas.put("mitkz", kjkmBean.getMitkz());
        datas.put("altkt", kjkmBean.getAltkt());
        datas.put("xintb", kjkmBean.getXintb());
        datas.put("erdat", kjkmBean.getErdat());
        datas.put("xloeb", kjkmBean.getXloeb());
        datas.put("ktopl", kjkmBean.getKtopl());
        datas.put("land1", kjkmBean.getLand1());
        datas.put("kokrs", kjkmBean.getKokrs());
        datas.put("xsalh", kjkmBean.getXsalh());
        datas.put("fstag", kjkmBean.getFstag());
        datas.put("uname", kjkmBean.getUname());
        datas.put("mwskz",kjkmBean.getMwskz());
        ModeRightInfo mode = new ModeRightInfo();
        String formmodeid = "";
        String sql = " select k.id from modeinfo k  \n" +
                " inner join workflow_bill l on formid = l.id\n" +
                " where l.tablename = 'uf_KJKM' ";

        rs.execute(sql);
        if (rs.next()) {
            formmodeid = Util.null2String(rs.getString("id"));
        }
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
        String currentdate = formatter1.format(new Date());
        String currenttime = formatter2.format(new Date());
        String modedatacreater = "1";
        String modedatacreatertype = "0";
        datas.put("formmodeid", formmodeid);
        datas.put("modedatacreater", modedatacreater);
        datas.put("modedatacreatertype", modedatacreatertype);
        datas.put("modedatacreatedate", currentdate);
        datas.put("modedatacreatetime", currenttime);

        String modeuuid = UUID.randomUUID().toString();
        datas.put("modeuuid", modeuuid);


        if (zflag == null || "".equals(zflag)) {
            message.setId("-200");
            message.setFlag("E");
            message.setMessage("业务标识字段zflag值为空");
        } else if ("1".equals(zflag)) {
//            新增
            writeLog("开始执行新增功能代码");
            //判断传递的会计科目编号在数据库中是否存在
            String saknr = kjkmBean.getSaknr();
            writeLog("传递的总账科目编号saknr是：" + saknr);
            if (saknr == null || "".equals(saknr)) {
                message.setId("-200");
                message.setFlag("E");
                message.setMessage("总账科目编号为空，无法进行新增");
            } else {
                //insertSQL组装
                String tablename = "" ;
                String tablenvalue = "" ;
                for (Map.Entry<String, Object> entry : datas.entrySet()){
                    String fieldname = Util.null2String(entry.getKey()).trim();
                    String fieldvalue = Util.null2String(entry.getValue()).replace("'", "''").trim();
                    tablename += tablename==""? fieldname :","+fieldname ;
                    tablenvalue += tablenvalue=="" ? "'"+fieldvalue+"'" :",'"+fieldvalue+"'" ;
                }
                if(tablename !="" && tablenvalue !=""){
                    int count = 0;
                    sql = " insert into uf_KJKM("+tablename+") values ("+ tablenvalue +")";
                    writeLog("sql:"+sql);
                    boolean boo = rs.executeUpdate(sql);
                    if(boo){
                        count++;
                    }
                    if(count >0){
                        String dataid = "" ;
                        sql = " select * from uf_KJKM where modeuuid='"+modeuuid+"' " ;
                        rs.execute(sql);
                        if(rs.next()){
                            dataid = Util.null2String(rs.getString("id"));
                        }
                        if(!"".equals(dataid)){
                            mode.editModeDataShare(Integer.valueOf(modedatacreater), Integer.parseInt(formmodeid), Integer.parseInt(dataid));
                        }
                        message.setFlag("S");
                        message.setMessage("新增数据成功!");
                        message.setId("200");
                    }else{
                        message.setFlag("E");
                        message.setMessage("新增数据失败!");
                        message.setId("-200");
                    }
                }
            }
        }else if ("2".equals(zflag) || "3".equals(zflag)){
            writeLog("开始执行更新或删除功能代码");
            String saknr = kjkmBean.getSaknr();
            writeLog("传递的总账科目编号saknr是：" + saknr);
            if (saknr == null || "".equals(saknr)) {
                message.setId("-200");
                message.setFlag("E");
                message.setMessage("总账科目编号为空，无法进行修改或删除");
            } else {
                Set<Map.Entry<String, Object>> entries = datas.entrySet();
                String partInsert = "";
                for (Map.Entry<String, Object> entry : entries) {
                    String fieldname = Util.null2String(entry.getKey()).trim();
                    String fieldvalue = Util.null2String(entry.getValue()).replace("'", "''").trim();
                    partInsert += fieldname+"='"+fieldvalue+"',";
                }
                if(partInsert.charAt(partInsert.length()-1) == ',') {
                    partInsert = partInsert.substring(0, partInsert.length()-1);
                }
                if (partInsert != ""){
                    sql = "UPDATE uf_KJKM set "+partInsert+" where saknr = '"+saknr+"'";
                    writeLog("sql:"+sql);
                    boolean updateResult = rs.executeUpdate(sql);
                    if (updateResult){
                        message.setFlag("S");
                        message.setMessage("处理成功!");
                        message.setId("200");
                    }else {
                        message.setFlag("E");
                        message.setMessage("处理失败!");
                        message.setId("-200");
                    }
                }
            }
        }
        return message;
    }

}


